<?php
/**
 * Plugin to setup generic application stuff.
 *
 * @category Zend
 * @package Init
 * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc.
 * @version $Id$
 * @link http://framework.zend.com
 * @since Available since release 1.0
 */

final class Disputeo_Controller_Plugin_Init extends Zend_Controller_Plugin_Abstract
{
    /**
     * @var Zend_Controller_Front
     */
    protected $_view;
    
    /**
     * @var Zend_Controller_Front
     */
    protected $_layoutView;
    
    /**
     * @var array
     */
    protected $_tabActions = array(
        'top',
        'new',
        'open'
    );
    
    /**
     * Plugin setup
     */
    protected function _init()
    {
        $this->_view = Zend_Controller_Front::getInstance()
            ->getParam('bootstrap')
            ->getResource('view');
        
        $this->_layoutView = Zend_Controller_Front::getInstance()
            ->getParam('bootstrap')
            ->getResource('layout')
            ->getView();
    }
    
    /**
     * Setup view and request properties
     * 
     * @param Zend_Controller_Request_Abstract $request 
     */
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $this->_init();
        $path = $request->getPathInfo();
        $this->_view->path = $path;
        $this->_layoutView->path = $path;
        $this->_layoutView->app = Disputeo_Config::config()->app;
        
        $session = Disputeo_Session::getSession();
        $this->_layoutView->session = $session;
        $this->_view->session = $session;
        $request->setParam('session', $session);
        
        if ($session->username) {
            $this->_tabActions[] = 'post';
        }
        
        if ($request->disputeId) {
            $dispute = Disputeo_Model_DisputeMapper::findOne($request->disputeId);
            $request->setParam('dispute', $dispute);
            $this->_view->dispute = $dispute;
        }
        
        if ($request->category) {
            $tabValidator = new Zend_Validate_InArray($this->_tabActions);
            if ($tabValidator->isValid($request->category)) {
                $request->setParam('active', $request->category);
                $request->setParam('category', $request->getParam('cat'));
            }
            $category = ucfirst($request->category);
            $request->setParam('category', null);
            $categories = Disputeo_Cache::classCache('Application_Model_Category')->getList();
            
            if ($request->postcat && isset($categories[$request->postcat])) {
                $category = $categories[$request->postcat];
            }
            
            $validator = new Zend_Validate_InArray($categories);
            if ($validator->isValid($category)) {
                $categories = array_flip($categories);
                $request->setParam('categoryId', $categories[$category]);
                $request->setParam('category', $category);
            }
       }
    }
}